﻿namespace BeatEmUp.World
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using BeatEmUp.Menu;
    using BeatEmUp.Characters.Enemies;
    using BeatEmUp.HUD;
    using BeatEmUp.Items;
    using BeatEmUp.Items.Weapons;
    using BeatEmUp.Enums;
    using Microsoft.Xna.Framework.Content;

    class ScrollingScreen
    {
        private GameManager gameManager;
        private Screen screen;
        private Player target;
        private EnemiesManager enemies;
        private WeaponsManager weapons;
        private float timeSinceLastMove = 0f;

        public ScrollingScreen(GameManager gameManager, Screen screen, Player target, EnemiesManager enemies, WeaponsManager weapons)
        {
            this.gameManager = gameManager;
            this.screen = screen;
            this.target = target;
            this.enemies = enemies;
            this.weapons = weapons;
        }

        public void LoadContent(ContentManager content)
        {
            this.screen.LoadContent(content);
        }

        public Screen getWorld()
        {
            return screen;
        }

        public bool ReachedEnd()
        {
            return !screen.CanScroll();
        }

        public void Update(GameTime gameTime)
        {
            if (this.enemies.EnemiesList.Count > 0)
                this.HandleStaticMode(gameTime);
            else
                this.HandleScrollMode(gameTime);

            this.screen.Update(gameTime);
        }

        protected void HandleScrollMode(GameTime gameTime)
        {
            int speed = 5;
            float howFar = (float)this.target.GetPosition().X / this.gameManager.WorldWidth;

            if (howFar < 0.5f || !this.screen.CanScroll())
                return;

            if (1 < (this.timeSinceLastMove += (float)gameTime.TotalGameTime.TotalSeconds))
            {
                this.timeSinceLastMove -= 1;
                this.MoveShitAround(speed);
            }

        }

        protected void MoveShitAround(int speed)
        {
            this.screen.MoveBy(speed, Direction.Right);

            foreach (Weapon weapon in this.weapons.weapons)
            {
                weapon.Move(speed, Direction.Left);
            }

            Rectangle targetPosition = this.target.GetPosition();
            targetPosition.X += -speed;
            this.target.SetPosition(targetPosition);
        }

        protected void HandleStaticMode(GameTime gameTime)
        {
            this.timeSinceLastMove = 0;
        }

        public void Draw(GameTime gameTime)
        {
            this.screen.Draw(gameTime);
        }
    }
}
